package com.club.subject.domain.service.impl;

import com.club.subject.common.enums.IsDeletedFlagEnum;
import com.club.subject.domain.convert.SubjectLabelConverter;
import com.club.subject.domain.entity.SubjectLabelBO;
import com.club.subject.domain.service.SubjectLabelDomainService;
import com.club.subject.infra.baisc.entity.SubjectLabel;
import com.club.subject.infra.baisc.entity.SubjectMapping;
import com.club.subject.infra.baisc.service.SubjectLabelService;
import com.club.subject.infra.baisc.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: sfy
 * @Date: 2024/4/12 17:30
 * @Description:
 */
@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    /**
     * 新增标签
     *
     * @param
     * @return
     */
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }

    public Boolean update(SubjectLabelBO subjectLabelBO) {
        //将bo转化为po
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }


    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        //将bo转化为po
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    /**
     * 查询分类下标签
     *
     * @param subjectLabelBO
     * @return
     */
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        //先获取分类id
        Long categoryId = subjectLabelBO.getCategoryId();
        //新建一个融合对象
        SubjectMapping subjectMapping = new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        //通过联合表(mapping表)去查出所有符合条件的mapping对象
        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
        }
        //将mapping对象中的标签id提取出来
        List<Long> labelIdlList = mappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
        //在label表中通过查出来的id定位符合要求的label对象
        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdlList);
        //将po对象转化为bo对象
        List<SubjectLabelBO> boList = new ArrayList<>();
        //遍历label集合
        labelList.forEach(label -> {
            SubjectLabelBO bo = new SubjectLabelBO();
            bo.setId(label.getId());
            bo.setCategoryId(categoryId);
            bo.setLabelName(label.getLabelName());
            bo.setSortNum(label.getSortNum());
            boList.add(bo);
        });
        return boList;
    }
}
